//
//  CJSCollectViewController.m
//  GoTraveling
//
//  Created by 陈金山 on 16/1/4.
//  Copyright © 2016年 开发华佗. All rights reserved.
//

#import "CJSCollectViewController.h"
#import "CJSDataBase.h"
#import "CJSLikeList.h"
#import "CJSStoryViewController.h"
#import "CJSTravelNotesViewController.h"

@interface CJSCollectViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) UIColor *backgColor;

@property (nonatomic, retain) UIColor *textColor;

@property (nonatomic, retain) UIView *backgView;

@end

@implementation CJSCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.backgColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.96 alpha:1];
    
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
//        
//        self.backgColor = [UIColor colorWithRed:0.15 green:0.14 blue:0.16 alpha:1];
//        self.textColor = [UIColor whiteColor];
//    }
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.frame = CGRectMake(0, 0, 50, 30);
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = self.backgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"collectCell"];
    [self.view addSubview:self.tableView];
    
    self.backgView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.backgView.backgroundColor = self.backgColor;
    [self.view addSubview:self.backgView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    label.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"您还没有收藏任何内容";
    label.textColor = self.textColor;
    [self.backgView addSubview:label];
    
}

- (void)editAction:(UIButton *)button {
    
    button.selected = !button.isSelected;
    if (button.isSelected) {
        [self.tableView setEditing:YES animated:YES];
    }
    else {
        [self.tableView setEditing:NO animated:YES];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSLikeList *list = [self.array objectAtIndex:indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [[CJSDataBase shareDataBase] deleteData:list.detail type:list.type];
        [self.array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.array.count == 0) {
        self.backgView.hidden = NO;
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
        [self.navigationItem.rightBarButtonItem setEnabled:NO];
    }
    else {
        self.backgView.hidden = YES;
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
        [self.navigationItem.rightBarButtonItem setEnabled:YES];
    }
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = self.backgColor;
    cell.textLabel.textColor = self.textColor;
    cell.textLabel.text = [[self.array objectAtIndex:indexPath.row] title];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSLikeList *likeList = [self.array objectAtIndex:indexPath.row];
    
        CJSStoryViewController *story = [[CJSStoryViewController alloc] init];
        story.spot_id = likeList.detail;
        [self.navigationController pushViewController:story animated:YES];
 
        
//        CJSTravelNotesViewController *travelNVC = [[CJSTravelNotesViewController alloc] init];
//        travelNVC.spot_id = likeList.detail;
//        [self.navigationController pushViewController:travelNVC animated:YES];

}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)bar {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [[CJSDataBase shareDataBase] openDB];
    self.array = [[CJSDataBase shareDataBase] selectData];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
